//
//  WBEmoticonPageView.swift
//  Weibo
//
//  Created by xu on 2017/3/30.
//  Copyright © 2017年 xuantong. All rights reserved.
//

import UIKit
private let CELLID = "reuseIdentifier"

class WBEmoticonPageView: UICollectionView {
    // 创建一个可变的 数组 保存image
    var imageList:[UIImage] = [UIImage]()
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        // 定义layout
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = CGSize(width: ScreenWidth, height:  216 - 35)
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        //滚动方向
          flowLayout.scrollDirection = .horizontal

        super.init(frame: frame, collectionViewLayout: flowLayout)
         setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //MARK: 供外界调用的方法 
     func addImage(image: UIImage){
        // 显示配图
        isHidden = false
        // 添加到数组中
        imageList.insert(image, at: 0)
        // 只能保存9张图片 如果大于9张 删除最后一张
        if imageList.count == 9 {
            imageList.removeLast()
        }
        // 刷新
        reloadData()

        
        
    }
    //MARK: 设置视图
    private func setupUI(){
               
        dataSource = self
        //注册
        register(WBEmoticonPageViewCell.self, forCellWithReuseIdentifier: CELLID)
        
        
    }
}





//数据源方法
extension WBEmoticonPageView: UICollectionViewDataSource{

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return WBEmoticonTools.shared.allEmoticons.count
    }

    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return WBEmoticonTools.shared.allEmoticons[section].count
    }

    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CELLID, for: indexPath)as!WBEmoticonPageViewCell
    
                // 赋值一维数组
        cell.emoticons = WBEmoticonTools.shared.allEmoticons[indexPath.section][indexPath.item]
    
        return cell
    }
    
    
}


